package com.example.myapplication.ic;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.example.myapplication.callback.LoginCallBack;

/**
 * Created by panyijun on 16/3/2.
 */
public class RegisterInterfaceImpl implements  RegisterInterface{

    //注册
    @Override
    public void getRegister(final LoginCallBack listener,String name,String email,String pass,String id) {



        RequestParams params = new RequestParams();
        params.addBodyParameter("teacher_name",name);
        params.addBodyParameter("teacher_email",email);
        params.addBodyParameter("teacher_password",pass);
        params.addBodyParameter("teacher_number",id);
        params.addBodyParameter("teacher_pic","/pic/head.png");
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.POST,
                "http://192.168.191.1/app/index.php/teacher/reg", params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        listener.onSuccess(responseInfo.result);
                        Log.i("res", responseInfo.result);
                    }

                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        //// TODO: 2015/12/18  ∂
                        listener.onFail(msg);
                    }


                });



    }
    //判断是否可以注册的前提
    @Override
    public boolean judgeIsRegister(Context context, EditText userName, EditText password, EditText email, EditText teacherId) {
        userName.setError(null);
        password.setError(null);
        email.setError(null);
        teacherId.setError(null);

        String name = userName.getText().toString().trim();
        String pass = password.getText().toString().trim();
        String em = email.getText().toString().trim();
        String tId = teacherId.getText().toString().trim();

        boolean cancel = false;
        View focusView = null;


        if (TextUtils.isEmpty(pass)) {
            password.setError("密码不能为空");
            focusView = password;
            cancel = true;
        }
        if (TextUtils.isEmpty(em)) {
            email.setError("邮箱不能为空");
            focusView = email;
            cancel = true;
        }
        if (TextUtils.isEmpty(tId)) {
            teacherId.setError("教师ID不能为空");
            focusView = teacherId;
            cancel = true;
        }
        if(em.indexOf("@")<0){
            email.setError("此邮箱无效");
            focusView = email;
            cancel = true;
        }
        if (TextUtils.isEmpty(name)) {
            userName.setError("用户名不能为空");
            focusView = userName;
            cancel = true;
        }

        // Check for a valid password, if the user entered one.
        if (!TextUtils.isEmpty(pass) && !isPasswordValid(pass)) {
            password.setError("密码太短");
            focusView = password;
            cancel = true;
        }


        if (cancel) {
            focusView.requestFocus();
        } else {
        }
        return cancel;
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }
}
